package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import com.android.kotlincoroutinemvvmdemo.R

/*
* 箭头刷新view
* */
class DoubleArrowRefreshView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context, attributeSet) {

    private var mWidth = 0
    private var mHeight = 0

    private val degreeArr = floatArrayOf(0F, 180F)

    private var sideLength = 0F

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //拐角---(尖角)
            strokeJoin = Paint.Join.MITER
            strokeWidth = context.resources.getDimension(R.dimen.dp_2)
            color = context.resources.getColor(R.color.color_00B0FF)
            style = Paint.Style.FILL_AND_STROKE
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            //拐角---(尖角)
            strokeJoin = Paint.Join.MITER
            strokeWidth = context.resources.getDimension(R.dimen.dp_2)
            color = context.resources.getColor(R.color.color_00B0FF)
            style = Paint.Style.STROKE
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    private val mPath1 by lazy {
        Path()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = when(MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_60).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when(MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_60).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)

        sideLength =  context.resources.getDimension(R.dimen.dp_6)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth / 2.0F
            val cy = mHeight / 2.0F
            translate(cx, cy)
            for (degree in degreeArr) {
                save()
                rotate(degree)
                mPath0.apply {
                    moveTo(
                        0F,
                        cy * 5 / 6 - cy / 10
                    )
                    lineTo(0F, cy * 5 / 6)
                    lineTo(
                       0F + cy / 10,
                        cy * 5 / 6,
                    )
                    close()
                }
                drawPath(mPath0, mPaint0)

                mPath1.apply {
                    moveTo(
                       0F + cy / 10 / 2,
                        cy * 5 / 6 - cy / 10 / 2
                    )
                    rQuadTo(
                        cx * 23 / 22,
                        -cy / 2,
                        cx / 4,
                        -cy * 32 / 21
                    )
                }
                drawPath(mPath1, mPaint1)
                restore()
            }
        }
    }
}